package com.palmwindActivity.textmodule;

import java.util.Vector;
import java.util.concurrent.ThreadPoolExecutor;

import com.palmwindActivity.app.AbstractServiceProxy;
import com.palmwindActivity.app.ICommConstants;
import com.palmwindActivity.app.IServiceProxyConstants;
import com.palmwindActivity.app.IServiceProxyListener;
import com.palmwindActivity.app.RequestParam;
import com.palmwindActivity.proto.ProtoBuf;
import com.palmwindActivity.proto.TextUpdate;
import com.palmwindActivity.proto.TextUpdate.TextUpdateResp;
import com.palmwindActivity.proto.TextUpdate.TextUpdateResp.TextComment;

public class TextUpdateProxy extends AbstractServiceProxy {

	protected Vector<UserComment> userComments;
	
	public TextUpdateProxy(ThreadPoolExecutor threadpool,
			IServiceProxyListener serverListener) {
		super(threadpool, serverListener);
		
		userComments = new Vector<UserComment>();
	}

	@Override
	protected void addRequestArgs(Vector<ProtoBuf> request,
			RequestParam requestItem) {
		if(requestItem.params.size() == 2)
		{
			String userName = (String)requestItem.params.get(0);
			Long timeStamp = (Long)requestItem.params.get(1);
			
			TextUpdate.TextUpdateReq.Builder builder = TextUpdate.TextUpdateReq.newBuilder();
			builder.setTimeStamp(timeStamp.longValue());
			builder.setAction(IServiceProxyConstants.ACTION_GET_TEXT);
			builder.setUsername(userName);
			TextUpdate.TextUpdateReq req = builder.build();
			
			ProtoBuf pb = new ProtoBuf();
            pb.setBufferData(req.toByteArray());
            pb.setObjType(IServiceProxyConstants.ACTION_GET_TEXT);
			request.add(pb);
		}
		
	}
	
	public void requireUpdateComments(String userName, long timeStamp)
	{
		RequestParam param = new RequestParam(IServiceProxyConstants.ACTION_GET_TEXT, this);
		param.params = new Vector();
		param.params.add(userName);
		param.params.add(Long.valueOf(timeStamp));
		
		Vector requestVector= this.createProtoBufReq(param);
		if(requestVector != null && requestVector.size() > 0)
		{
			sendData(requestVector, IServiceProxyConstants.ACTION_GET_TEXT, 15000, 0);
		}
	}

	protected void handleResponse(ProtoBuf[] response) {
		try {
			if (response != null) {
				int size = response.length;
				for (int i = 0; i < size; i++) {
					TextUpdate.TextUpdateResp textUpdateResp = TextUpdate.TextUpdateResp
							.parseFrom(response[i].getBufferData());
					parseTextUpdateResp(textUpdateResp);
				}

			}
		} catch (Throwable t) {
			this.status = ICommConstants.NETWORK_PARSER_ERROR;
			t.printStackTrace();
		}

	}

	private void parseTextUpdateResp(TextUpdateResp textUpdateResp) {
		// TODO Auto-generated method stub
		if(textUpdateResp != null)
		{
			int size = textUpdateResp.getCommentsCount();
			for(int i = 0; i < size; i ++)
			{
				UserComment textComment = new UserComment();
				TextComment protoTextComment = textUpdateResp.getComments(i);
				long timeStamp = protoTextComment.getTimeStamp();
				String userName = protoTextComment.getUsername();
				String comment = protoTextComment.getComments();
				textComment.setTimeStamp(timeStamp);
				textComment.setComment(comment);
				textComment.setUserName(userName);
				userComments.add(textComment);
			}
		}
	}
	
	public Vector<UserComment> getComments()
	{
		return userComments;
	}

}
